<% add_decidim_page_title(t("impersonatable_users", scope: "decidim.admin.titles")) %>
<% if current_organization.available_authorizations.empty? %>
  <div>
    <%= cell("decidim/announcement", t(".needs_authorization_warning"), callout_class: "warning" ) %>
  </div>
<% end %>

<div class="item_show__header">
  <h1 class="item_show__header-title">
    <%= t "decidim.admin.titles.impersonatable_users" %>
    <% if allowed_to? :impersonate, :impersonatable_user, user: new_managed_user %>
      <%= link_to t(".impersonate_new_managed_user"), new_impersonatable_user_impersonation_path(:new_managed_user), class: "button button__sm button__secondary #{"disabled" if current_organization.available_authorizations.empty?}" %>
    <% end %>
  </h1>
</div>

<div class="filters__section">
  <div class="relative">
    <button class="button button__xs button__transparent-secondary" data-controller="dropdown" data-target="dropdown-filters">
      <%= t("filter_label", scope: "decidim.admin.filters") %>
      <%= icon "arrow-down-s-line", class: "!fill-secondary" %>
      <%= icon "arrow-down-s-line", class: "!fill-secondary" %>
    </button>

    <ul class="dropdown" id="dropdown-filters" aria-hidden="true">
      <li class="dropdown__item">
        <%= link_to t(".filter.managed"), url_for(state: "managed", q: @query), class: "dropdown__button" %>
      </li>
      <li class="dropdown__item">
        <%= link_to t(".filter.not_managed"), url_for(state: "not_managed", q: @query), class: "dropdown__button" %>
      </li>
      <li class="dropdown__item">
        <%= link_to t(".filter.all"), url_for(q: @query), class: "dropdown__button" %>
      </li>
    </ul>
  </div>
  <div class="fcell search">
    <%= form_tag "", method: :get do %>
      <div class="input-group">
        <%= search_field_tag :q, @query,label: false, class: "input-group-field", placeholder: t(".search") %>
        <%= hidden_field_tag :state, @state %>
        <div class="input-group-button">
          <button type="submit" class="text-secondary" aria-label="<%= t("decidim.search.term_input_placeholder") %>">
            <%= icon "search-line", class: "fill-secondary w-4 h-4" %>
          </button>
        </div>
      </div>
    <% end %>
  </div>
</div>

<div class="card">
  <div class="table-stacked">
    <table class="table-list">
      <thead>
        <tr>
          <th><%= t(".name") %></th>
          <th><%= t(".status") %></th>
          <th><%= t("decidim.admin.actions.actions") %></th>
        </tr>
      </thead>
      <tbody>
        <% @users.each do |user| %>
          <tr data-user-id="<%= user.id %>">
            <td data-label="<%= t(".name") %>">
              <%= user.name %>
            </td>
            <td data-label="<%= t(".status") %>">
              <%= user.managed? ? t(".managed") : t(".not_managed") %>
            </td>
            <td class="table-list__actions" data-label="<%= t("decidim.admin.actions.actions") %>">
              <button type="button" data-controller="dropdown" data-target="actions-user-<%= user.id %>" aria-label="<%= t("decidim.admin.actions.actions_label", resource: user.name) %>">
                <%= icon "more-fill", class: "text-secondary" %>
              </button>

              <div class="inline-block relative">
                <ul id="actions-user-<%= user.id %>" class="dropdown dropdown__action" aria-hidden="true">
                  <% if allowed_to? :impersonate, :impersonatable_user, user: user %>
                    <li class="dropdown__item">
                      <%= link_to new_impersonatable_user_impersonation_path(user), class: "dropdown__button" do %>
                        <%= icon "user-line" %>
                        <%= t(".impersonate") %>
                      <% end %>
                    </li>

                    <hr>
                  <% end %>

                  <li class="dropdown__item">
                    <%= link_to impersonatable_user_impersonation_logs_path(user), class: "dropdown__button" do %>
                      <%= icon "time-line" %>
                      <%= t(".view_logs") %>
                    <% end %>
                  </li>

                  <% if allowed_to? :promote, :impersonatable_user, user: user %>
                    <hr>

                    <li class="dropdown__item">
                      <%= link_to new_impersonatable_user_promotion_path(user), class: "dropdown__button" do %>
                        <%= icon "award-line" %>
                        <%= t(".promote") %>
                      <% end %>
                    </li>
                  <% end %>
                </ul>
              </div>
            </td>
          </tr>
        <% end %>
      </tbody>
    </table>
  </div>
</div>
<%= decidim_paginate @users %>
